package com.leet.code.lcof;

import java.util.HashMap;

public class Code48 {

    public static void main(String[] args) {
        System.out.println(new Code48().lengthOfLongestSubstring("pwwkew"));
    }

    public int lengthOfLongestSubstring(String s) {
        HashMap<Character, Integer> map = new HashMap<>();
        int max = 0;
        int l = 0;
        int start = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            Integer j = map.get(c);
            if (j == null) {
                l++;
                if (l > max) {
                    max = l;
                }
            } else {
                for (; start <= j; start++) {
                    map.remove(s.charAt(start));
                }
                l = i - j;
            }
            map.put(c, i);

        }
        return max;
    }

}
